package com.sdau.repairmaster.UIFile;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.sdau.repairmaster.R;

import org.json.JSONException;
import org.json.JSONObject;

import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;


public class ForgetPwdActivity_two extends AppCompatActivity implements View.OnClickListener, TextWatcher {

    private ImageView ivBack;//返回按钮
    private EditText etPhoneNumber;//获取手机号
    private ImageView ivPhoneNumberDel;//清空手机号
    private EditText etCode;//获取验证码
    private Button btnSendCode;//发送验证码
    private Button btnCheckCode;//检查验证码,提交按钮

    private String phone;//手机号
    private String code;//验证码
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_retrieve_pwd);
        initView();//初始化控件
        registerListener();//绑定监听器
        smssdk();

    }
    //初始化控件
    public void initView(){
        ivBack =findViewById(R.id.ib_navigation_back);
        etPhoneNumber = findViewById(R.id.et_retrieve_tel);
        ivPhoneNumberDel = findViewById(R.id.iv_retrieve_tel_del);
        etCode = findViewById(R.id.et_retrieve_code_input);
        btnSendCode = findViewById(R.id.retrieve_sms_call);
        btnCheckCode = findViewById(R.id.bt_retrieve_submit);

        phone = getIntent().getStringExtra("phone");
        etPhoneNumber.setText(phone);
        etPhoneNumber.setEnabled(false);

    }
    //封装回调机制
    public void smssdk(){
        EventHandler handler = new EventHandler(){
            @Override
            public void afterEvent(int event, int result, Object data) {

//                result = SMSSDK.RESULT_COMPLETE;
//                event = SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE;

                if (result == SMSSDK.RESULT_COMPLETE){

                    //回调完成
                    if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
                        //提交验证码成功
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(ForgetPwdActivity_two.this,"验证成功",Toast.LENGTH_SHORT).show();
                                startActivity(new Intent(ForgetPwdActivity_two.this, ResetPwdActivity.class).
                                        putExtra("phone",phone));//跳转到重置密码页面
                                finish();
                            }
                        });
                    }else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE){
                        //获取验证码成功
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(ForgetPwdActivity_two.this,"验证码已发送",Toast.LENGTH_SHORT).show();
                            }
                        });
                    }else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES){
                        Log.i("test","test");
                    }
                }else{
                    ((Throwable)data).printStackTrace();
                    Throwable throwable = (Throwable) data;
                    throwable.printStackTrace();
                    Log.i("1234",throwable.toString());
                    try {
                        JSONObject obj = new JSONObject(throwable.getMessage());
                        final String des = obj.optString("detail");
                        if (!TextUtils.isEmpty(des)){
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(ForgetPwdActivity_two.this,des,Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        SMSSDK.registerEventHandler(handler);
    }
    //注册监听器
    public void registerListener(){
        //注册点击事件
        ivBack.setOnClickListener(ForgetPwdActivity_two.this);
        btnSendCode.setOnClickListener(ForgetPwdActivity_two.this);
        btnCheckCode.setOnClickListener(ForgetPwdActivity_two.this);
        ivPhoneNumberDel.setOnClickListener(ForgetPwdActivity_two.this);

        //注册其他事件
        etPhoneNumber.addTextChangedListener(ForgetPwdActivity_two.this);
        etCode.addTextChangedListener(ForgetPwdActivity_two.this);
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.ib_navigation_back:
                finish();
                break;
            case R.id.iv_retrieve_tel_del:
                etPhoneNumber.setText(null);
                break;
            case R.id.retrieve_sms_call:
                phone = etPhoneNumber.getText().toString();
                if(!TextUtils.isEmpty(phone.trim())){
                    if(phone.trim().length()==11){
                        SMSSDK.getVerificationCode("86",phone);
                        etCode.requestFocus();
                    }else{
                        Toast.makeText(ForgetPwdActivity_two.this, "请输入完整电话号码", Toast.LENGTH_SHORT).show();
                        etPhoneNumber.requestFocus();
                    }
                }else{
                    Toast.makeText(ForgetPwdActivity_two.this, "请输入您的电话号码", Toast.LENGTH_SHORT).show();
                    etPhoneNumber.requestFocus();
                }
                break;
            case R.id.bt_retrieve_submit:
                code = etCode.getText().toString();
                if(!TextUtils.isEmpty(code.trim())){
                    if(code.trim().length()==6){
                        SMSSDK.submitVerificationCode("86", phone, code);

                    }else{
                        Toast.makeText(ForgetPwdActivity_two.this, "请输入完整验证码", Toast.LENGTH_LONG).show();
                        etCode.requestFocus();
                    }
                }else{
                    Toast.makeText(ForgetPwdActivity_two.this, "请输入验证码", Toast.LENGTH_LONG).show();
                    etCode.requestFocus();
                }


                break;

        }
    }
    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }
    //设置清除图片是否显示
    @Override
    public void afterTextChanged(Editable editable) {
        String phone = etPhoneNumber.getText().toString().trim();
        String code = etCode.getText().toString().trim();

        if(phone.length() > 0){
            ivPhoneNumberDel.setVisibility(View.VISIBLE);
        }else{
            ivPhoneNumberDel.setVisibility(View.INVISIBLE);
        }

        //判断确认按钮是否可用
        if(phone.length() == 11 && code.length() == 6){
            btnCheckCode.setBackgroundResource(R.drawable.bg_login_submit);
            btnCheckCode.setTextColor(getResources().getColor(R.color.white));
        }else{
            btnCheckCode.setBackgroundResource(R.drawable.bg_login_submit_lock);
            btnCheckCode.setTextColor(getResources().getColor(R.color.login_input_active));
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        SMSSDK.unregisterAllEventHandler();
    }


}
